/*
Content: Generate dataset with treatments based on pre-post setup as described in Figure F.1.
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


*********************************
***** MONTHLY EVENTS ************
*********************************
	
use "$globalterror/intermediate/allevents_monthly_ieb_final.dta", clear 	
		
* For each country in the data expand to have a panel month-year for 1970 to 2018
tab year
drop if startmy==.
keep ieb_nat_id
bys ieb_nat_id: gen unique = _n
keep if unique == 1 


expand 588 // (2018-1970+1)*12=588

	* Create variable startmy for all month-year combinations from 1970 to 2019
	gen counter = 1 
	bys ieb_nat_id: gen startmy = 120	// 120 is Jan1970 in the month-year number classification
	bys ieb_nat_id: gen spell = _n - 1 
	bys ieb_nat_id: replace startmy = startmy + spell 
	format startmy %tm		
	
	* Merge back the original event data keeping only the variables of interest
	merge 1:1 startmy ieb_nat_id using "$globalterror/intermediate/allevents_monthly_ieb_final.dta", gen(_merge_terror) keepusing(ieb_nat_id startmy terror year month) keep(1 3)


cap drop year 
cap drop month
g year=year(dofm(startmy))
g month=month(dofm(startmy))


* If in a given month there is no event assign a zero	
foreach var in  terror     {
replace `var' = 0 if _merge_terror == 1 // months with no terror
		
}
	
	
*******************************************
*** RELEVANT AND ISOLATED TERROR EVENTS ***
*******************************************

* Yearly totals
foreach var in  terror    {
	bys year ieb_nat_id: egen `var'_y = total(`var') 
}
	
* RELEVANCE: Difference with respect to the yearly average in the 3, 4 and 5 years before
foreach var in  terror     {
			
			* Generate yearly averages (not total)
			bys year ieb_nat_id: egen `var'_ya = mean(`var')
			
			* Generate difference with respect to the yearly average of the previous three years
			bys ieb_nat_id (startmy): gen `var'_m3y = (`var' - ((`var'_ya[_n-12] + `var'_ya[_n-24] + `var'_ya[_n-36])/3))
			
			* Generate difference with respect to the yearly average of the previous four years 
			bys ieb_nat_id (startmy): gen `var'_m4y = (`var' - ((`var'_ya[_n-12] + `var'_ya[_n-24] + `var'_ya[_n-36]+ `var'_ya[_n-48])/4))
			
			* Generate difference with respect to the yearly average of the previous five years
			bys ieb_nat_id (startmy): gen `var'_m5y = (`var' - ((`var'_ya[_n-12] + `var'_ya[_n-24] + `var'_ya[_n-36]+ `var'_ya[_n-48]+ `var'_ya[_n-60])/5))
}


	* There is at least 1 relevant terror event
	foreach event in  terror  {	
	foreach var in	 m5y m4y m3y {
	g event`event'_`var'=`event'_`var'>=1 
		}
		}	
		

	drop _merge_terror
	drop unique counter spell
	drop if year<1987
	compress


* ISOLATE: To use multiple events in one year, we need to isolate those events around time bands
bysort ieb_nat_id year: g obs=_n
cap drop months_year
bysort ieb_nat_id (startmy): g months_year=_n

*This accumulates the events over the past 1 month, 2 months and 3 months
cap drop x 
foreach event in  terror   {	
foreach var in	 m5y m4y m3y {
		
	rangestat (sum) event`event'_`var'_p30=event`event'_`var', interval(months_year,-1,0) by(ieb_nat_id)
replace event`event'_`var'_p30=. if year==1987 & month==1
	
	rangestat (sum) event`event'_`var'_p60=event`event'_`var', interval(months_year,-2,0) by(ieb_nat_id)
replace event`event'_`var'_p60=. if year==1987 & inrange(month,1,2)

	rangestat (sum) event`event'_`var'_p90=event`event'_`var', interval(months_year,-3,0) by(ieb_nat_id)
replace event`event'_`var'_p90=. if year==1987 & inrange(month,1,3)
			
}
}


* Identify "isolated" events, e.g. months were in the previous 1, 2 or 3 months there were no events
foreach event in  terror  {	
foreach var in	 m5y m4y m3y {
g keep_e`event'_`var'_p30=event`event'_`var'_p30<=1
g keep_e`event'_`var'_p60=event`event'_`var'_p60<=1
g keep_e`event'_`var'_p90=event`event'_`var'_p90<=1		
}
}


* Identify the date of these isolated events
foreach event in  terror   {	
foreach var in	 m5y m4y m3y {
g date_e`event'_`var'_p30=startmy if event`event'_`var'==1 & keep_e`event'_`var'_p30==1
g date_e`event'_`var'_p60=startmy if event`event'_`var'==1 & keep_e`event'_`var'_p60==1
g date_e`event'_`var'_p90=startmy if event`event'_`var'==1 & keep_e`event'_`var'_p90==1			
}
}
format date_e*_p*0 %tm

* Allocates the date of isolated events to the pre-and post-reference bands
foreach event in  terror   {	
foreach var in	 m5y m4y m3y {
rangestat (min) relevant`event'_`var'_p30=date_e`event'_`var'_p30 if keep_e`event'_`var'_p30==1, interval(startmy, -1, 1) by(ieb_nat_id)
rangestat (min) relevant`event'_`var'_p60=date_e`event'_`var'_p60 if keep_e`event'_`var'_p60==1, interval(startmy, -2, 2) by(ieb_nat_id)
rangestat (min) relevant`event'_`var'_p90=date_e`event'_`var'_p90 if keep_e`event'_`var'_p90==1, interval(startmy, -3, 3) by(ieb_nat_id)
}
}
format relevant*_p*0 %tm

* Creates the distance in months from the isolated event
foreach event in  terror   {	
foreach var in	 m5y m4y m3y {
g distance`event'_`var'_p30=startmy- relevant`event'_`var'_p30 if relevant`event'_`var'_p30!=.
g distance`event'_`var'_p60=startmy- relevant`event'_`var'_p60 if relevant`event'_`var'_p60!=.
g distance`event'_`var'_p90=startmy- relevant`event'_`var'_p90 if relevant`event'_`var'_p90!=.
}
}


* Make sure that for every isolated event there is a control and treatment group within the corresponding bandwidth
foreach event in  terror    {	
foreach var in	 m5y m4y m3y {
g select`event'_`var'_p30=1 if date_e`event'_`var'_p30!=. & distance`event'_`var'_p30[_n-1]==-1 & distance`event'_`var'_p30[_n+1]==1
g select`event'_`var'_p60=1 if date_e`event'_`var'_p60!=. & distance`event'_`var'_p60[_n-1]==-1 & distance`event'_`var'_p60[_n+1]==1 & distance`event'_`var'_p60[_n-2]==-2 & distance`event'_`var'_p60[_n+2]==2
g select`event'_`var'_p90=1 if date_e`event'_`var'_p90!=. & distance`event'_`var'_p90[_n-1]==-1 & distance`event'_`var'_p90[_n+1]==1 & distance`event'_`var'_p90[_n-2]==-2 & distance`event'_`var'_p90[_n+2]==2 & distance`event'_`var'_p90[_n-3]==-3 & distance`event'_`var'_p90[_n+3]==3
}
}

foreach event in  terror  {	
foreach var in	 m5y m4y m3y {
replace date_e`event'_`var'_p30=. if select`event'_`var'_p30!=1
replace date_e`event'_`var'_p60=. if select`event'_`var'_p60!=1
replace date_e`event'_`var'_p90=. if select`event'_`var'_p90!=1		
}
}

foreach event in terror    {	
foreach var in	 m5y m4y m3y {

cap drop relevant`event'_`var'_p30 relevant`event'_`var'_p60 relevant`event'_`var'_p90
rangestat (min) relevant`event'_`var'_p30=date_e`event'_`var'_p30 if keep_e`event'_`var'_p30==1, interval(startmy, -1, 1) by(ieb_nat_id)
rangestat (min) relevant`event'_`var'_p60=date_e`event'_`var'_p60 if keep_e`event'_`var'_p60==1, interval(startmy, -2, 2) by(ieb_nat_id)
rangestat (min) relevant`event'_`var'_p90=date_e`event'_`var'_p90 if keep_e`event'_`var'_p90==1, interval(startmy, -3, 3) by(ieb_nat_id)

}
}
format relevant*_p*0 %tm

* Create the distance in months from the isolated event
foreach event in  terror   {	
foreach var in	 m5y m4y m3y {
cap drop  distance`event'_`var'_p30 distance`event'_`var'_p60 distance`event'_`var'_p90
g distance`event'_`var'_p30=startmy- relevant`event'_`var'_p30 if relevant`event'_`var'_p30!=.
g distance`event'_`var'_p60=startmy- relevant`event'_`var'_p60 if relevant`event'_`var'_p60!=.
g distance`event'_`var'_p90=startmy- relevant`event'_`var'_p90 if relevant`event'_`var'_p90!=.
}
}


* Create an event identifier - combine date and country
foreach var in	 m5y m4y m3y {
sort ieb_nat_id startmy
cap drop identterror_`var'_p90
g identterror_`var'_p90=string(ieb_nat_id)+"_"+string(relevantterror_`var'_p90) if relevantterror_`var'_p90!=. & distanceterror_`var'_p90!=. // event identifier
}



compress
save "$globalterror/intermediate/terror_monthlydata_ieb_final.dta", replace 



* Clean dataset: minimum necessary to merge withe large IEB data
drop if ieb_nat_id==.
drop if year <2000


	* This keeps the date of the actual isolated event
	foreach x in	30 60 90 {
	foreach event in  terror {	
	foreach var in	 m5y m4y m3y {
	gen obs`event'_`var'_p`x' =1 if startmy==relevant`event'_`var'_p`x'
	}
	}
	}
	* Keep date only when there is terror event
	keep if obsterror_m5y_p30==1 |  obsterror_m4y_p30==1 | obsterror_m3y_p30==1 | obsterror_m5y_p60==1 |  obsterror_m4y_p60==1 | obsterror_m3y_p60==1 | obsterror_m5y_p90==1 |  obsterror_m4y_p90==1 | obsterror_m3y_p90==1


	* Keep only useful variables
	keep ieb_nat_id startmy obs* terror terror_m3y terror_m4y terror_m5y
	order  ieb_nat_id startmy obs*
	rename obs* rel_isol_*

save "$globalterror/intermediate\relevantisolatedeventsmonthly_ieb_final.dta", replace
	

********************************************************************************
**# LOAD DATASET OF YEAR-MONTH DATES FROM 1999 TO 2020
********************************************************************************

clear

* Upload dataset with Month-Year dates 
use  "$globalterror/help\year_monthdate.dta"

* Expand to have month-year for each country of origin
expand 999 
bysort date: g obs=_n
{/*labels*/
label define STN_BNG -9 `"          -9 Keine Zuordnung möglich"', modify
label define STN_BNG -8 `"          -8 Fehler im Ursprungswert"', modify
label define STN_BNG -7 `"          -7 Keine Angabe"', modify
label define STN_BNG -6 `"          -6 Trifft nicht zu"', modify
label define STN_BNG -5 `"          -5 (leer)"', modify
label define STN_BNG 0 `"           0 Deutschland"', modify
label define STN_BNG 7 `"           7 unbekannt (ehem. Arberlsvf)"', modify
label define STN_BNG 121 `"         121 Albanien"', modify
label define STN_BNG 122 `"         122 Bosnien und Herzegowina"', modify
label define STN_BNG 123 `"         123 Andorra"', modify
label define STN_BNG 124 `"         124 Belgien"', modify
label define STN_BNG 125 `"         125 Bulgarien"', modify
label define STN_BNG 126 `"         126 Dänemark"', modify
label define STN_BNG 127 `"         127 Estland"', modify
label define STN_BNG 128 `"         128 Finnland"', modify
label define STN_BNG 129 `"         129 Frankreich"', modify
label define STN_BNG 130 `"         130 Kroatien"', modify
label define STN_BNG 131 `"         131 Slowenien"', modify
label define STN_BNG 132 `"         132 Serbien und Montenegro"', modify
label define STN_BNG 133 `"         133 Serbien"', modify
label define STN_BNG 134 `"         134 Griechenland"', modify
label define STN_BNG 135 `"         135 Irland"', modify
label define STN_BNG 136 `"         136 Island"', modify
label define STN_BNG 137 `"         137 Italien"', modify
label define STN_BNG 138 `"         138 Jugoslawien"', modify
label define STN_BNG 139 `"         139 Lettland"', modify
label define STN_BNG 140 `"         140 Montenegro"', modify
label define STN_BNG 141 `"         141 Liechtenstein"', modify
label define STN_BNG 142 `"         142 Litauen"', modify
label define STN_BNG 143 `"         143 Luxemburg"', modify
label define STN_BNG 144 `"         144 Mazedonien"', modify
label define STN_BNG 145 `"         145 Malta"', modify
label define STN_BNG 146 `"         146 Moldau"', modify
label define STN_BNG 147 `"         147 Monaco"', modify
label define STN_BNG 148 `"         148 Niederlande"', modify
label define STN_BNG 149 `"         149 Norwegen"', modify
label define STN_BNG 150 `"         150 Kosovo"', modify
label define STN_BNG 151 `"         151 Österreich"', modify
label define STN_BNG 152 `"         152 Polen"', modify
label define STN_BNG 153 `"         153 Portugal"', modify
label define STN_BNG 154 `"         154 Rumänien"', modify
label define STN_BNG 155 `"         155 Slowakei"', modify
label define STN_BNG 156 `"         156 San Marino"', modify
label define STN_BNG 157 `"         157 Schweden"', modify
label define STN_BNG 158 `"         158 Schweiz"', modify
label define STN_BNG 159 `"         159 Sowjetunion"', modify
label define STN_BNG 160 `"         160 Russische Föderation"', modify
label define STN_BNG 161 `"         161 Spanien"', modify
label define STN_BNG 162 `"         162 Tschechoslowakei"', modify
label define STN_BNG 163 `"         163 Türkei"', modify
label define STN_BNG 164 `"         164 Tschechien"', modify
label define STN_BNG 165 `"         165 Ungarn"', modify
label define STN_BNG 166 `"         166 Ukraine"', modify
label define STN_BNG 167 `"         167 Vatikanstadt"', modify
label define STN_BNG 168 `"         168 Großbritannien und Nordirland"', modify
label define STN_BNG 169 `"         169 Weißrussland"', modify
label define STN_BNG 170 `"         170 Serbien"', modify
label define STN_BNG 181 `"         181 Zypern"', modify
label define STN_BNG 185 `"         185 Britische Überseegebiete"', modify
label define STN_BNG 195 `"         195 abhängige Gebiete"', modify
label define STN_BNG 199 `"         199 übriges Europa"', modify
label define STN_BNG 221 `"         221 Algerien"', modify
label define STN_BNG 223 `"         223 Angola"', modify
label define STN_BNG 224 `"         224 Eritrea"', modify
label define STN_BNG 225 `"         225 Äthiopien"', modify
label define STN_BNG 226 `"         226 Lesotho"', modify
label define STN_BNG 227 `"         227 Botsuana"', modify
label define STN_BNG 229 `"         229 Benin"', modify
label define STN_BNG 230 `"         230 Dschibuti"', modify
label define STN_BNG 231 `"         231 Cote dIvoire"', modify
label define STN_BNG 232 `"         232 Nigeria"', modify
label define STN_BNG 233 `"         233 Simbabwe"', modify
label define STN_BNG 236 `"         236 Gabun"', modify
label define STN_BNG 237 `"         237 Gambia"', modify
label define STN_BNG 238 `"         238 Ghana"', modify
label define STN_BNG 239 `"         239 Mauretanien"', modify
label define STN_BNG 242 `"         242 Kap Verde"', modify
label define STN_BNG 243 `"         243 Kenia"', modify
label define STN_BNG 244 `"         244 Komoren"', modify
label define STN_BNG 245 `"         245 Kongo"', modify
label define STN_BNG 246 `"         246 Kongo, Dem. Republik"', modify
label define STN_BNG 247 `"         247 Liberia"', modify
label define STN_BNG 248 `"         248 Libyen"', modify
label define STN_BNG 249 `"         249 Madagaskar"', modify
label define STN_BNG 251 `"         251 Mali"', modify
label define STN_BNG 252 `"         252 Marokko"', modify
label define STN_BNG 253 `"         253 Mauritius"', modify
label define STN_BNG 254 `"         254 Mosambik"', modify
label define STN_BNG 255 `"         255 Niger"', modify
label define STN_BNG 256 `"         256 Malawi"', modify
label define STN_BNG 257 `"         257 Sambia"', modify
label define STN_BNG 258 `"         258 Burkina Faso"', modify
label define STN_BNG 259 `"         259 Guinea-Bissau"', modify
label define STN_BNG 261 `"         261 Guinea"', modify
label define STN_BNG 262 `"         262 Kamerun"', modify
label define STN_BNG 263 `"         263 Südafrika"', modify
label define STN_BNG 265 `"         265 Ruanda"', modify
label define STN_BNG 267 `"         267 Namibia"', modify
label define STN_BNG 268 `"         268 Sao Tomé und Principe"', modify
label define STN_BNG 269 `"         269 Senegal"', modify
label define STN_BNG 271 `"         271 Seychellen"', modify
label define STN_BNG 272 `"         272 Sierra Leone"', modify
label define STN_BNG 273 `"         273 Somalia"', modify
label define STN_BNG 274 `"         274 Äquatorialguinea"', modify
label define STN_BNG 276 `"         276 Sudan (einschl. Südsudan)"', modify
label define STN_BNG 277 `"         277 Sudan"', modify
label define STN_BNG 278 `"         278 Südsudan"', modify
label define STN_BNG 281 `"         281 Swasiland"', modify
label define STN_BNG 282 `"         282 Vereinigte Republik Tansania"', modify
label define STN_BNG 283 `"         283 Togo"', modify
label define STN_BNG 284 `"         284 Tschad"', modify
label define STN_BNG 285 `"         285 Tunesien"', modify
label define STN_BNG 286 `"         286 Uganda"', modify
label define STN_BNG 287 `"         287 Ägypten"', modify
label define STN_BNG 289 `"         289 Zentralafrikanische Republik"', modify
label define STN_BNG 291 `"         291 Burundi"', modify
label define STN_BNG 295 `"         295 St. Helena"', modify
label define STN_BNG 299 `"         299 übriges Afrika"', modify
label define STN_BNG 320 `"         320 Antigua und Barbuda"', modify
label define STN_BNG 322 `"         322 Barbados"', modify
label define STN_BNG 323 `"         323 Argentinien"', modify
label define STN_BNG 324 `"         324 Bahamas"', modify
label define STN_BNG 326 `"         326 Bolivien"', modify
label define STN_BNG 327 `"         327 Brasilien"', modify
label define STN_BNG 328 `"         328 Guyana"', modify
label define STN_BNG 330 `"         330 Belize"', modify
label define STN_BNG 332 `"         332 Chile"', modify
label define STN_BNG 333 `"         333 Dominica"', modify
label define STN_BNG 334 `"         334 Costa Rica"', modify
label define STN_BNG 335 `"         335 Dominikanische Republik"', modify
label define STN_BNG 336 `"         336 Ecuador"', modify
label define STN_BNG 337 `"         337 El Salvador"', modify
label define STN_BNG 340 `"         340 Grenada"', modify
label define STN_BNG 345 `"         345 Guatemala"', modify
label define STN_BNG 346 `"         346 Haiti"', modify
label define STN_BNG 347 `"         347 Honduras"', modify
label define STN_BNG 348 `"         348 Kanada"', modify
label define STN_BNG 349 `"         349 Kolumbien"', modify
label define STN_BNG 351 `"         351 Kuba"', modify
label define STN_BNG 353 `"         353 Mexiko"', modify
label define STN_BNG 354 `"         354 Nicaragua"', modify
label define STN_BNG 355 `"         355 Jamaika"', modify
label define STN_BNG 357 `"         357 Panama"', modify
label define STN_BNG 359 `"         359 Paraguay"', modify
label define STN_BNG 361 `"         361 Peru"', modify
label define STN_BNG 364 `"         364 Suriname"', modify
label define STN_BNG 365 `"         365 Uruguay"', modify
label define STN_BNG 366 `"         366 St. Lucia"', modify
label define STN_BNG 367 `"         367 Venezuela"', modify
label define STN_BNG 368 `"         368 Vereinigte Staaten"', modify
label define STN_BNG 369 `"         369 St. Vincent und die Grenadinen"', modify
label define STN_BNG 370 `"         370 St. Kitts und Nevis"', modify
label define STN_BNG 371 `"         371 Trinidad und Tobago"', modify
label define STN_BNG 395 `"         395 abhängige Gebiete"', modify
label define STN_BNG 399 `"         399 übriges Amerika"', modify
label define STN_BNG 411 `"         411 Hongkong"', modify
label define STN_BNG 412 `"         412 Macau"', modify
label define STN_BNG 421 `"         421 Jemen"', modify
label define STN_BNG 422 `"         422 Armenien"', modify
label define STN_BNG 423 `"         423 Afghanistan"', modify
label define STN_BNG 424 `"         424 Bahrain"', modify
label define STN_BNG 425 `"         425 Aserbaidschan"', modify
label define STN_BNG 426 `"         426 Bhutan"', modify
label define STN_BNG 427 `"         427 Myanmar"', modify
label define STN_BNG 429 `"         429 Brunei Darussalam"', modify
label define STN_BNG 430 `"         430 Georgien"', modify
label define STN_BNG 431 `"         431 Sri Lanka"', modify
label define STN_BNG 432 `"         432 Vietnam"', modify
label define STN_BNG 434 `"         434 Dem. Volksrepublik Korea"', modify
label define STN_BNG 436 `"         436 Indien"', modify
label define STN_BNG 437 `"         437 Indonesien"', modify
label define STN_BNG 438 `"         438 Irak"', modify
label define STN_BNG 439 `"         439 Islamische Republik Iran"', modify
label define STN_BNG 441 `"         441 Israel"', modify
label define STN_BNG 442 `"         442 Japan"', modify
label define STN_BNG 443 `"         443 Dem. Volksrepublik Jemen"', modify
label define STN_BNG 444 `"         444 Kasachstan"', modify
label define STN_BNG 445 `"         445 Jordanien"', modify
label define STN_BNG 446 `"         446 Kambodscha"', modify
label define STN_BNG 447 `"         447 Katar"', modify
label define STN_BNG 448 `"         448 Kuwait"', modify
label define STN_BNG 449 `"         449 Dem. Volksrepublik Laos"', modify
label define STN_BNG 450 `"         450 Kirgisistan"', modify
label define STN_BNG 451 `"         451 Libanon"', modify
label define STN_BNG 454 `"         454 Malediven"', modify
label define STN_BNG 456 `"         456 Oman"', modify
label define STN_BNG 457 `"         457 Mongolei"', modify
label define STN_BNG 458 `"         458 Nepal"', modify
label define STN_BNG 459 `"         459 Palästinensische Gebiete"', modify
label define STN_BNG 460 `"         460 Bangladesch"', modify
label define STN_BNG 461 `"         461 Pakistan"', modify
label define STN_BNG 462 `"         462 Philippinen"', modify
label define STN_BNG 465 `"         465 Taiwan"', modify
label define STN_BNG 467 `"         467 Republik Korea"', modify
label define STN_BNG 468 `"         468 Dem. Volksrepublik Korea"', modify
label define STN_BNG 469 `"         469 Vereinigte Arabische Emirate"', modify
label define STN_BNG 470 `"         470 Tadschikistan"', modify
label define STN_BNG 471 `"         471 Turkmenistan"', modify
label define STN_BNG 472 `"         472 Saudi-Arabien"', modify
label define STN_BNG 474 `"         474 Singapur"', modify
label define STN_BNG 475 `"         475 Arabische Republik Syrien"', modify
label define STN_BNG 476 `"         476 Thailand"', modify
label define STN_BNG 477 `"         477 Usbekistan"', modify
label define STN_BNG 479 `"         479 China"', modify
label define STN_BNG 481 `"         481 Zypern"', modify
label define STN_BNG 482 `"         482 Malaysia"', modify
label define STN_BNG 483 `"         483 Timor-Leste"', modify
label define STN_BNG 495 `"         495 Hongkong"', modify
label define STN_BNG 499 `"         499 übriges Asien"', modify
label define STN_BNG 523 `"         523 Australien"', modify
label define STN_BNG 524 `"         524 Salomonen"', modify
label define STN_BNG 525 `"         525 Nördliche Marianen"', modify
label define STN_BNG 526 `"         526 Fidschi"', modify
label define STN_BNG 527 `"         527 Cookinseln"', modify
label define STN_BNG 530 `"         530 Kiribati"', modify
label define STN_BNG 531 `"         531 Nauru"', modify
label define STN_BNG 532 `"         532 Vanuatu"', modify
label define STN_BNG 533 `"         533 Niue"', modify
label define STN_BNG 536 `"         536 Neuseeland"', modify
label define STN_BNG 537 `"         537 Palau"', modify
label define STN_BNG 538 `"         538 Papua-Neuguinea"', modify
label define STN_BNG 540 `"         540 Tuvalu"', modify
label define STN_BNG 541 `"         541 Tonga"', modify
label define STN_BNG 543 `"         543 Samoa"', modify
label define STN_BNG 544 `"         544 Marshallinseln"', modify
label define STN_BNG 545 `"         545 Föder. Staaten v. Mikronesien"', modify
label define STN_BNG 595 `"         595 Pitcairn-Insel"', modify
label define STN_BNG 599 `"         599 übriges Ozeanien"', modify
label define STN_BNG 994 `"         994 von/nach See"', modify
label define STN_BNG 996 `"         996 unbekanntes Ausland"', modify
label define STN_BNG 997 `"         997 staatenlos"', modify
label define STN_BNG 998 `"         998 ungeklärt"', modify
label define STN_BNG 999 `"         999 ohne Angabe"', modify
}
rename date startmy
rename obs ieb_nat_id
label val ieb_nat_id STN_BNG
replace ieb_nat_id=0 if ieb_nat_id==1

* Drop numbers with no associated to a country
drop if inrange(ieb_nat_id,2,6)
drop if inrange(ieb_nat_id,171,180)
drop if inrange(ieb_nat_id,186,194)
drop if inrange(ieb_nat_id,200,220)
drop if inrange(ieb_nat_id,300,319)
drop if inrange(ieb_nat_id,372,394)
drop if inrange(ieb_nat_id,400,410)
drop if inrange(ieb_nat_id,484,494)
drop if inrange(ieb_nat_id,500,522)
drop if inrange(ieb_nat_id,546,594)
drop if inrange(ieb_nat_id,600,993)

********************************************************************************
**# MERGE EACH DATE-COUNTRT TO THE RELEVANT & ISOLATED EVENTS DATASET
********************************************************************************

merge 1:1 startmy ieb_nat_id using "$globalterror/intermediate\relevantisolatedeventsmonthly_ieb_final.dta", gen(merge_relterror)


* Drop countries with no event between 1999-2020
keep startmy ieb_nat_id rel_isol*

foreach var in	 m5y m4y m3y {
	
bysort ieb_nat_id: egen max_event`var'=max(rel_isol_terror_`var'_p90)
drop if max_event`var'==.

sort ieb_nat_id startmy
tsset ieb_nat_id startmy


foreach x in 1 2 3 {
bysort ieb_nat_id (startmy): g temp`x'=F`x'.rel_isol_terror_`var'_p90
replace temp`x'=-`x' if temp`x'==1
}

g treatperiod_terror_`var'_p90=0 if rel_isol_terror_`var'_p90==1
foreach x in 1 2 3 {
replace treatperiod_terror_`var'_p90= temp`x' if treatperiod_terror_`var'_p90==. & temp`x'!=.
} 

g treatment_terror_`var'_p90=0 if treatperiod_terror_`var'_p90==-3
replace treatment_terror_`var'_p90=1 if treatperiod_terror_`var'_p90==0

drop if treatperiod_terror_`var'_p90==.
drop  temp* rel_isol_terror_`var'_p90  max_event`var'

}

save "$globalterror/final\prepost_setup_smonthly_ieb_final.dta", replace

